package com.ys.controller.web.finance;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.service.finance.TcwWanliReconciliationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.config.MyConfig;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.finance.TcwWanliReconciliation;
import com.ys.entity.finance.TcwWanliReconciliationItem;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;

@Controller
@RequestMapping("/web/finance/tcwWanliReconciliation")
public class WebTcwWanliReconciliationController {
	
	@Autowired
	private CommonApiService<TcwWanliReconciliation> commonApiService;

	@Autowired
	private TcwWanliReconciliationService tcwWanliReconciliationService;
	
	/*
	 * 新增
	 */
	@RequiresPermissions("tcwWanliReconciliation:add")
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){

		try {
			String bid = commonApiService.getCommonNextBid(MenuId.TCW_WanliReconciliation);
			
			TcwWanliReconciliation entity = new TcwWanliReconciliation()
					.setBid(bid)
					.setCreateTime(new Date());
			
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_WanliReconciliation, new ArrayList<Map<String, Object>>());
			
			String aa = commonApiService.toAdd(model, entity, MenuId.TCW_WanliReconciliation, null, subGridConfig);

            if ("test".equals(MyConfig.getActive()) || "pro".equals(MyConfig.getActive())) {
    			return "finance/tcwWanliReconciliation/toAdd";
			} else {
				return aa;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 编辑
	 */
	@RequiresPermissions("tcwWanliReconciliation:add")
	@GetMapping("/toFlow")
	public String toFlow(Model model, HttpServletRequest request, HttpServletResponse response,
			String bid){
		if (bid == null || bid.length() == 0) {
			return W.error(model, "单号不能为空");
		}
		
		TcwWanliReconciliation entity = new TcwWanliReconciliation().setBid(bid).queryFirst();

		try {
			Map<String, Object> extraParam = tcwWanliReconciliationService.getExtraParam(bid);
			// 子列表配置信息
			List<Map<String, Object>> sonlist = tcwWanliReconciliationService.getItemList(bid);
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_WanliReconciliation, sonlist);
			
			String aa = commonApiService.toEdit(model, entity.getId(), new TcwWanliReconciliation(), MenuId.TCW_WanliReconciliation, extraParam, subGridConfig);

            if ("test".equals(MyConfig.getActive()) || "pro".equals(MyConfig.getActive())) {
    			return "finance/tcwWanliReconciliation/toEdit";
			} else {
				return aa;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, "数据有误，请联系客服");
		}
	}
}